//
//  STOSRViewCornerViewController.swift
//  iPhone
//
//  Created by Toj on 3/7/24.
//
import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STOSRViewCornerViewController: UIViewController {
    
    private var vc_category = "STOSR"
    
    private lazy var scrollView: UIScrollView = {
        let view = UIScrollView()
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var cornerView  = setupCornerView()
    private lazy var cornerView2 = setupCornerView()
    private lazy var cornerView3 = setupCornerView()
    private lazy var cornerView4 = setupCornerView()
    private lazy var cornerView5 = setupCornerView()
    private lazy var cornerView6 = setupCornerView()
    private lazy var cornerView7 = setupCornerView()
    private lazy var cornerView8 = setupCornerView()
    private lazy var cornerView9 = setupCornerView()
    private lazy var cornerView10 = setupCornerView()
    private lazy var cornerView11 = setupCornerView()
    private lazy var cornerView12 = setupCornerView()
    private lazy var cornerView13 = setupCornerView()
    private lazy var cornerView14 = setupCornerView()
    private lazy var cornerView15 = setupCornerView()
    private lazy var cornerView16 = setupCornerView()
    private lazy var cornerView17 = setupCornerView()
    private lazy var cornerView18 = setupCornerView()
    private lazy var cornerView19 = setupCornerView()
    private lazy var cornerView20 = setupCornerView()
    private lazy var cornerView21 = setupCornerView()
    private lazy var cornerView22 = setupCornerView()
    private lazy var cornerView23 = setupCornerView()
    private lazy var cornerView24 = setupCornerView()
    private lazy var cornerView25 = setupCornerView()
    private lazy var cornerView26 = setupCornerView()
    private lazy var cornerView27 = setupCornerView()
    private lazy var cornerView28 = setupCornerView()
    private lazy var cornerView29 = setupCornerView()
    private lazy var cornerView30 = setupCornerView()
    
    func setupCornerView() -> UIView {
        let view = UIView()
        view.backgroundColor = .random
        return view
    }
    
    private let top = 25.to_cgfloat
    private let margin = 10.to_cgfloat
    private let spacing = 10.to_cgfloat
    
    private let innerColors = [UIColor.random, UIColor.random]
    private let outerColors = [UIColor.random, UIColor.random]
    
    private let cls = 3
    private lazy var cwh: CGFloat = {
        let fcls = cls.to_cgfloat
        let value = (CGFloat.dv_width - spacing * (fcls-1) - margin * 2) / fcls
        return value
    }()
    
    private lazy var wh1_2: CGFloat = { return cwh * 0.5 }()
    private lazy var wh1_4: CGFloat = { return cwh * 0.25 }()
    
    private lazy var contentViews: [UIView] = {
        let views =
        [
            cornerView28, cornerView29, cornerView30,
            cornerView,   cornerView2,  cornerView3,
            cornerView4,  cornerView5,  cornerView6,
            cornerView7,  cornerView8,  cornerView9,
            cornerView10, cornerView11, cornerView12,
            cornerView13, cornerView14, cornerView15,
            cornerView16, cornerView17, cornerView18,
            cornerView19, cornerView20, cornerView21,
            cornerView22, cornerView23, cornerView24,
            cornerView25, cornerView26, cornerView27
        ]
        return views
    }()
    
    deinit {
        print("-->startimes: \(st_className) func: deinit")
    }
}

// MARK: - 生命周期
extension STOSRViewCornerViewController {
    
    override var view: UIView! {
        set {
            CM.begin(time: st_objectName + vc_category)
            super.view = newValue
        }
        get { return super.view }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeInitialize()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        _ = CM.end(time: st_objectName)
        _ = CM.end(time: st_objectName + vc_category)
    }
}

// MARK: - 初始化设置
private extension STOSRViewCornerViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(scrollView)
        
        let views = contentViews
        views.forEach { scrollView.addSubview($0) }
    }
    
    func setupMakeLayoutSubviews() {
        scrollView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(view)
            make.top.equalTo(CGFloat.nav_barFullHeight)
        }
        let views = contentViews
        let wh = cwh
        var x: CGFloat = 0
        var y: CGFloat = 0
        var index = 0
        views.forEach { subview in
            let rowIndex = index / cls
            let clsIndex = index % cls
            x = margin + (wh + spacing) * CGFloat(clsIndex)
            y = top + (wh + spacing) * CGFloat(rowIndex)
            subview.frame = CGRect(x: x, y: y, width: wh, height: wh)
            index += 1
        }
        let maxHeight = views.last?.frame.maxY ?? 0
        scrollView.contentSize = CGSize(width: .dv_width, height: maxHeight)
    }
    
    func setupMakeInitialize() {
        view.backgroundColor = .white
        
        setupViewCorners()
        setupViewCorners2()
        setupViewCorners3()
        setupViewCorners4()
        setupViewCorners5()
        setupViewCorners6()
        setupViewCorners7()
        setupViewCorners8()
        setupViewCorners9()
        setupViewCorners10()
        setupViewCorners11()
        setupViewCorners12()
        setupViewCorners13()
        setupViewCorners14()
        setupViewCorners15()
        setupViewCorners16()
        setupViewCorners17()
        setupViewCorners18()
        setupViewCorners19()
        setupViewCorners20()
        setupViewCorners21()
        setupViewCorners22()
        setupViewCorners23()
        setupViewCorners24()
        setupViewCorners25()
        setupViewCorners26()
        setupViewCorners27()
        setupViewCorners28()
        setupViewCorners29()
        setupViewCorners30()
    }
}

// MARK: - 设置圆角
private extension STOSRViewCornerViewController {
    
    // MARK: - 1. 无 子视图: 无 离屏
    /**
     1. 无 子视图: 无离屏
     */
    func setupViewCorners() {
        cornerView.cm_cornerRadius = cwh * 0.5
    }
    
    // MARK: - 2. 有 子视图: 有 离屏.
    /**
     2. 有 子视图: 有离屏.
     - NOTE: 设置父视图圆角, 子视图设置颜色/ UIlabel文本
     */
    func setupViewCorners2() {
        let _subView = UILabel()
        _subView.font = 10.to_boldFont
        _subView.textAlignment = .center
        _subView.text = "会触发: 离屏渲染"
        
        // let _subView = UIView()
        // _subView.backgroundColor = .red
        
        _subView.frame = CGRect(
            origin: CGPoint(x: 0, y: wh1_4),
            size: CGSize(width: cwh, height: wh1_2)
        )
        cornerView2.addSubview(_subView)
        cornerView2.cm_cornerRadius = wh1_2
    }
    
    // MARK: - 3. 有 子视图: 无 离屏
    /**
     3. 有 子视图: 无离屏.
     - NOTE: 子视图无颜色, 无内容
     */
    func setupViewCorners3() {
        let _subView = UIView()
        _subView.frame = CGRect(
            origin: CGPoint(x: 0, y: 0),
            size: CGSize(width: cwh, height: cwh)
        )
        cornerView3.addSubview(_subView)
        cornerView3.cm_cornerRadius = wh1_2
    }
    
    // MARK: - 4. 有 子视图: 无 离屏.
    /**
     4. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners4() {
        cornerView4.backgroundColor = .clear
        
        let _subView = UILabel()
        _subView.text = "不触发: 离屏渲染"
        _subView.font = 10.to_boldFont
        _subView.textAlignment = .center
        _subView.backgroundColor = .red
        _subView.frame = CGRect(
            origin: CGPoint(x: 0, y: 0),
            size: CGSize(width: cwh, height: cwh)
        )
        _subView.cm_cornerRadius = wh1_2
        cornerView4.addSubview(_subView)
    }
    
    // MARK: - 5. 有 子视图: 无 离屏.
    /**
     5. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners5() {
        let _subView = UILabel()
        _subView.text = "不触发: 离屏渲染"
        _subView.font = 10.to_boldFont
        _subView.textAlignment = .center
        _subView.backgroundColor = .red
        _subView.frame = CGRect(
            origin: CGPoint(x: 0, y: 0),
            size: CGSize(width: cwh, height: cwh)
        )
        _subView.cm_cornerRadius = wh1_2
        cornerView5.addSubview(_subView)
        
        cornerView5.backgroundColor = "#FFF3CC".to_color
        cornerView5.st_corner(
            cradius: wh1_2,
            shadow: "#D7B971".to_color,
            sopacity: 1,
            sradius: 2,
            soffset: CGSize(width: 0, height: 5)
        )
    }
    
    // MARK: - 6. 有 子视图: 无 离屏.
    /**
     6. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners6() {
        cornerView6.backgroundColor = .clear
        let colors = innerColors.map { return $0.cgColor }
        cornerView6.gd_add(colors: colors)
    }
    
    // MARK: - 7. 无 子视图: 无 离屏.
    /**
     7. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners7() {
        cornerView7.backgroundColor = .red
        let colors = innerColors.map { return $0.cgColor }
        cornerView7.gd_add(innerColors: colors, innerDirection: .vertical, outerWidth: 10, corner: wh1_2)
    }
    
    // MARK: - 8. 无 子视图: 有 离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners8() {
        cornerView8.backgroundColor = .clear
        let colors = innerColors.map { return $0.cgColor }
        cornerView8.gd_add(colors: colors, corner: wh1_2, borderColor: UIColor.random.cgColor, borderWidth: 10)
    }
    
    // MARK: - 9. 无 子视图: 无 离屏.
    /**
     9. 无 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners9() {
        let icolors = innerColors.map { return $0.cgColor }
        let ocolors = outerColors.map { return $0.cgColor }
        cornerView9.gd_add(innerColors: icolors, innerDirection: .horizontal, outerColors: ocolors, outerDirection: .horizontal, outerWidth: 10, corner: wh1_2)
    }
    
    // MARK: - 10. 无 子视图: 无 离屏.
    /**
     10. 无 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners10() {
        let icolors = innerColors.map { return $0.cgColor }
        let ocolors = outerColors.map { return $0.cgColor }
        cornerView10.gd_add(innerColors: icolors, outerColors: ocolors, outerWidth: 10)
    }

    // MARK: - 11. 有 子视图: 无离屏.
    /**
     11. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners11() {
        let icolors = innerColors.map { return $0.cgColor }
        //let ocolors = outerColors.map { return $0.cgColor }
        cornerView11.gd_add(
            innerColors: icolors,
            outerWidth: 10
        )
    }
    
    // MARK: - 12. 有 子视图: 无离屏.
    /**
     12. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners12() {
        //let icolors = innerColors.map { return $0.cgColor }
        let ocolors = outerColors.map { return $0.cgColor }
        cornerView12.gd_add(
            innerColors: [UIColor.brown.cgColor],
            outerColors: ocolors,
            outerWidth: 10)
    }
    
    // MARK: - 13. 有 子视图: 无离屏.
    /**
     13. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners13() {
        //let icolors = innerColors.map { return $0.cgColor }
        //let ocolors = outerColors.map { return $0.cgColor }
        cornerView13.gd_add(
            innerColors: [UIColor.purple.cgColor],
            outerWidth: 10)
    }
    
    // MARK: - 14. 有 子视图: 无离屏.
    /**
     14. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners14() {
        //let icolors = innerColors.map { return $0.cgColor }
        //let ocolors = outerColors.map { return $0.cgColor }
        cornerView14.gd_add(
            innerColors: [UIColor.random.cgColor],
            outerColors: [UIColor.random.cgColor],
            outerWidth: 10)
    }
    
    // MARK: - 15. 有 子视图: 无离屏.
    /**
     15. 有 子视图: 无离屏.
     - NOTE: 渐变; 阴影; 圆角;
     */
    func setupViewCorners15() {
        let swh = cwh - 10 * 3
        let icolors = innerColors.map { return $0.cgColor }
        let ocolors = outerColors.map { return $0.cgColor }
        
        let _subView = UILabel()
        _subView.text = "不触发: 离屏渲染"
        _subView.font = 10.to_boldFont
        _subView.textAlignment = .center
        _subView.backgroundColor = .clear
        _subView.frame = CGRect(
            origin: CGPoint(x: 15, y: 15),
            size: CGSize(width: swh, height: swh)
        )
        _subView.cm_cornerRadius = swh * 0.5
        cornerView15.addSubview(_subView)
        
        cornerView15.gd_add(
            innerColors: icolors,
            outerColors: ocolors,
            outerWidth: 10
            , corner: wh1_2
        )
        
        cornerView15.st_shadow(
            color: "#D7B971".to_color,
            opacity: 0.5,
            radius: 2,
            offset: CGSize(width: -5, height: -5)
            , corner: wh1_2
        )
    }
    
    // MARK: - 16. 有 子视图: 无离屏.
    /**
     16. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners16() {
        let _subView = UILabel()
        _subView.text = "不触发: 离屏渲染"
        _subView.font = 10.to_boldFont
        _subView.textAlignment = .center
        _subView.backgroundColor = .red
        _subView.frame = CGRect(
            origin: CGPoint(x: 0, y: 0),
            size: CGSize(width: cwh, height: cwh)
        )
        _subView.cm_cornerRadius = wh1_2
        
        cornerView16.addSubview(_subView)
        cornerView16.backgroundColor = "#FFF3CC".to_color
        cornerView16.st_shadow(
            color: "#D7B971".to_color,
            opacity: 1,
            radius: 2,
            offset: CGSize(width: 0, height: 5),
            corner: wh1_2
        )
    }
    
    // MARK: - 17. 有 子视图: 无离屏.
    /**
     17. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners17() {
        cornerView17.backgroundColor = "#FFF3CC".to_color
        cornerView17.st_shadow(
            color: "#D7B971".to_color,
            opacity: 1,
            radius: 2,
            offset: CGSize(width: 0, height: 5),
            corner: wh1_2
        )
    }
    
    // MARK: - 18. 有 子视图: 无离屏.
    /**
     18. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners18() {
        
        cornerView18.backgroundColor = .gray
        //let colors = innerColors.map { return $0.cgColor }
        
        let _layer = CALayer()
        _layer.frame = cornerView18.bounds
        _layer.backgroundColor = UIColor.red.cgColor
        _layer.cornerRadius = 10
        _layer.maskedCorners = .ca_layerTopCorner
        _layer.masksToBounds = true
        
        let renderer = UIGraphicsImageRenderer(size: cornerView18.bounds.size)
        let image = renderer.image { context in
            _layer.render(in: context.cgContext)
        }
        
        let contentsLayer = CALayer()
        contentsLayer.contents = image.cgImage
        contentsLayer.frame = cornerView18.bounds
        
        cornerView18.layer.addSublayer(contentsLayer)
    }
    
    // MARK: - 19. 有 子视图: 无离屏.
    /**
     19. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners19() {
        let icolors = innerColors.map { return $0.cgColor }
        
        let _subView = UIView()
        cornerView19.superview?.addSubview(_subView)
        var frame = cornerView19.frame
        frame.size.height = wh1_2
        _subView.frame = frame
        _subView.gd_add(colors: icolors)
        cornerView19.st_corner(cradius: 10)
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners20() {
        
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners21() {
        
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners22() {
        
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners23() {
        
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners24() {
        
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners25() {
        
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners26() {
        
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners27() {
        
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners28() {
        cornerView30.st_shadow(color: .red, opacity: 0.5, radius: 3, offset: CGSize(width: 5, height: 5))
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners29() {
        cornerView29.st_shadow(color: .red, opacity: 0.5, radius: 3, offset: CGSize(width: 5, height: 5))
    }
    
    @objc func didTapChange() {
        index2 += 1
        var frame = cornerView29.frame
        let i = (index2 % 2 == 0 ? 1 : -1)
        frame.size.width += 10 * i.to_cgfloat
        cornerView29.frame = frame
        cornerView29.st_shadow(color: .red, opacity: 0.5, radius: 3, offset: CGSize(width: 5, height: 5))
    }
    
    // MARK: - 8. 有 子视图: 无离屏.
    /**
     8. 有 子视图: 无离屏.
     - NOTE: 父视图无颜色, 子视图有颜色, 子视图 设置圆角
     */
    func setupViewCorners30() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTapChange))
        cornerView30.addGestureRecognizer(tap)
    }
}

var index2 =  0
